package musica;

import java.io.*;
import java.util.Map;
import javazoom.jlgui.basicplayer.*;
import javazoom.spi.mpeg.sampled.file.*;

public class Player implements BasicPlayerListener{

    private Cancion cancion;
    private BasicPlayer player;
    private int progreso;
    private boolean paused = false;
    long longitud;

    public Player() {
        player = new BasicPlayer();
        player.addBasicPlayerListener(this);
    }

    public void play() {
        try {
            if (cancion == null || !cancion.getDisponible()) {
                System.err.println("No hay cancion para hacer play!");
            } else {
                player.play();
            }
        } catch (BasicPlayerException ex) {
            System.err.println("Error haciendo play");
            ex.printStackTrace();
        }
    }

    public void play(String path) {
        try {
            Cancion c = new Cancion(path);
            this.open(path);
            player.play();
        } catch (BasicPlayerException ex) {
            System.err.println("Petacion haciendo play");
            ex.printStackTrace();
        }
    }
    
    /** Devuelve el BasicPlayer de este player */
    public BasicPlayer getBasicPlayer() {
        return player;
    }

    public void play(Cancion cancion) {
        try {
            this.open(cancion.getFile().toString());
            player.play();
        } catch (BasicPlayerException ex) {
            System.err.println("Petacion en play");
            ex.printStackTrace();
        }
    }

    public void togglePause() {
        if (paused) resume();
        else pause();
    }
    public void pause() {
        try {
            paused = true;
            player.pause();
        } catch (BasicPlayerException ex) {
            System.err.println("Error haciendo pause");
            ex.printStackTrace();
        }
    }

    public void resume() {
        try {
            paused = false;
            player.resume();

        } catch (BasicPlayerException ex) {
            System.err.println("Error haciendo resume");
            ex.printStackTrace();
        }
    }

    /** Salta al byte "offset" de la cancion. Si te pasas de frenada
     * te corrijo*/
    public void seek(long offset) {
        try {
            if (offset > cancion.getFile().length()) {
                offset = cancion.getFile().length() - 2;
            }

            player.seek(offset);

        } catch (BasicPlayerException ex) {
            System.err.println("Peto dando un salto");
        }
    }

    /** Salta al porcentaje de cancion dado por percent. 
     * El salto debe estar entre 0 y 1
     */
    public void seek(float percent) {
        try {
            if (percent >= 1) {
                percent = 0.99f;
            }

            Float offset = percent * cancion.getFile().length();
            player.seek(offset.longValue());
        } catch (BasicPlayerException ex) {
            System.err.println("Petacion haciendo un salto");
            ex.printStackTrace();
        }

    }

    public long getLongitud() {
        return this.longitud;
    }

    public int getProgreso () {
        return this.progreso;
    }
    
    /** Devuelve el volumen del reproductor. Valor entre
     * 0 y 1. A partir de 1/2 distorsiona */
    public float getVolumen() {
        return player.getGainValue();
    }

    public void setVolumen(float vol) {
        try {
            player.setGain(vol);
        } catch (BasicPlayerException ex) {
            System.err.println("Peto cambiando volumen");
        }
    }

    public Cancion getCancion() {
        return cancion;
    }

    public void open(String path) {
        try {
            player.open(new File(path));
            cancion = new Cancion(path,null);
            longitud = new File(path).length();
        } catch (BasicPlayerException ex) {
            System.err.println("Petacion abriendo");
            ex.printStackTrace();
        }
    }
    
    public void opened(Object arg0, Map arg1) {

    }

    public void progress(int arg0, long arg1, byte[] arg2, Map arg3) {
        // El codigo de victor va aqui
        float leido = (float) arg0/longitud;
        this.progreso = (int) (100*leido);
    }

    public void stateUpdated(BasicPlayerEvent arg0) {

    }

    public void setController(BasicController arg0) {

    }


}
